package com.briup.ch01;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/11/5
 * @Description:
 */
public class MyConnection {
    //维护建立连接的四要素
    private static String driverClass="com.mysql.cj.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/briup";
    private static String username="briup";
    private static String password="briup";
    private static Connection conn=null;
    //获取连接的方法
    public static Connection getConnection(){
        try{
            Class.forName(driverClass);
            conn= DriverManager.getConnection(url,username,password);

        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //关闭
    public static void closeConnection(){
        try {
            if(conn!=null){ conn.close(); }
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test(){
        System.out.println(getConnection());
        closeConnection();
    }

}
